package yns.springboot.guide.demo.data.consumer.rabbitmq.template;

import java.io.IOException;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import com.rabbitmq.client.Channel;

import yns.springboot.guide.demo.configure.data.rabbitmq.RabbitmqConfigure;

@Component
public class RabbitmqTemplateConsumer {
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private RabbitmqConfigure rabbitmqConfigure;

	@RabbitListener(queues = { "test" })
	@RabbitHandler()
	public void process(@Payload String body) {
		logger.info("process -- body : " + body);
	}

	@RabbitListener(queues = { "test", "test3" })
	@RabbitHandler()
	public void process2(@Payload String body, @Headers Map<String, Object> headers) {
		logger.info("process2 -- body : " + body);
		logger.info("process2 -- Headers : " + headers);
	}

	@RabbitListener(bindings = {
			@QueueBinding(exchange = @Exchange(value = "exchange.direct"), value = @Queue(value = "test2"), key = "test.info") })
	@RabbitHandler()
	public void process3(@Payload String body, @Headers Map<String, Object> headers, Message message, Channel channel)
			throws IOException {
		logger.info("process3 -- body : " + body);
		logger.info("process3 -- headers : " + headers);
		logger.info("process3 -- message : " + message);
		logger.info("process3 -- channel : " + channel.getChannelNumber());
		logger.info(" ---------------------------  ");
		
//		channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		
		
	}

}
